import app from '@/app'
import axios from '@/axios'
import { computed, reactive, UnwrapNestedRefs } from 'vue'
import { ensureDictionary, DictionaryEntry } from '@/cores/configurations/data-dictionary/useDictionary'
import { Entry, DataDictionary } from '@/utils/DataDictionary'

interface SpecialCase {
    Code: string,
    RealName: string,
    AllowTypes: string[],
    EndOn: string
}

interface ProjectOptionDetail {
    AllowTypes: string[],
    ApplyStart: string,
    ApplyEnd: string,
    SpecialCases: SpecialCase[]
}

interface ProjectOptions {
    ApplyYear: string,
    Batch: string,
    MaxPerBatch: Number,
    InnoVOption: ProjectOptionDetail,
    PrinFOption: ProjectOptionDetail,
    ValuEOption: ProjectOptionDetail,
    OpenLOption: ProjectOptionDetail,
    OptionSaveTo: Number,
    SaveKey: string,
    [props: string]: any
}

let msg = await axios.post<ProjectOptions>("/api/proj/opt/GetOption");
let option : UnwrapNestedRefs<ProjectOptions> = reactive(msg.data);

async function saveOption () {
    let msg = await axios.post<ProjectOptions>("/api/proj/opt/SaveOption", option);
    for (let key in option) {
        Object.assign(option[key], msg.data[key]);
    }
}

const yearDict = computed(() => {
    let dict = {
        name: "申请年度",
        entries: [
            {
                DataKey: "",
                DataValue: "请选择年度",
            }
        ]
    };

    let final = parseInt(option.ApplyYear);
    final = isNaN(final) || final < 2014 ? (new Date()).getFullYear() : final;
    for (let year = final; year >= 2014; year--) {
        let entry = {
            DataKey: year.toString(),
            DataValue: year.toString(),
        };
        dict.entries.push(entry)
    }

    return dict;
});

const batchDict = reactive({
    name: "批次",
    entries: [
        {
            DataKey: "",
            DataValue: "请选择批次",
        },
        {
            DataKey: "1",
            DataValue: "1",
        },
        {
            DataKey: "2",
            DataValue: "2",
        }
    ]
})


async function buildFullProcesDict () {
    let { entries } = await ensureDictionary("全过程状态");
    
    let dict : DataDictionary = {
        name: "全过程状态",
        entries: [
        ]
    };

    const toEntry = (entry : DictionaryEntry, group? : string) : Entry => {
        return {
            DataKey: entry.DataKey,
            DataValue: entry.DataValue,
            Group: group
        };
    };

    for (let item of entries) {
        let state = parseInt(item.DataKey);
        if (item.DataKey === "-1") {
            dict.entries.push(toEntry(item));
            dict.entries.push({
                DataKey: "-2",
                DataValue: "在研项目",
            });
        } else if (state < 1000) {
            dict.entries.push(toEntry(item, "项目申请状态组"));
        } else if (state < 2000) {
            dict.entries.push(toEntry(item, "中期检查状态组"));
        } else if (state < 3000) {
            dict.entries.push(toEntry(item, "项目延期状态组"));
        } else {
            dict.entries.push(toEntry(item, "项目结题状态组"));
        }
    }

    Object.assign(fullProcessDict, dict);
    return dict;
}
buildFullProcesDict();
const fullProcessDict = reactive({});

export default option;
export {
    saveOption,
    yearDict,
    batchDict,
    fullProcessDict
}